package com.xudow.app.dynamicstate_old.module.discovery;

import android.os.Bundle;
import android.support.annotation.NonNull;

import com.jude.beam.expansion.list.BeamListFragmentPresenter;
import com.xudow.app.dynamicstate_old.data.DynamicModel;
import com.xudow.app.dynamicstate_old.data.server.ErrorTransform;
import com.xudow.app.dynamicstate_old.domain.entity.Dynamic;

/**
 * Created by ske on 2016/6/11.
 */
public class DiscoverPresenter extends BeamListFragmentPresenter<DiscoverFragment, Dynamic> {
    long lastTime = 0;

    @Override
    protected void onCreate(@NonNull DiscoverFragment view, Bundle savedState) {
        super.onCreate(view, savedState);
        onRefresh();
    }

    @Override
    public void onRefresh() {
        DynamicModel.getInstance()
                .discoverDynamics(getView().getActivity(), 10, lastTime, 1)
                .doOnNext(list -> {
                    if (list != null && list.size() > 0)
                        lastTime = list.get(list.size() - 1).getPublishTime();
                })
                .doAfterTerminate(() -> getAdapter().notifyDataSetChanged())
                .unsafeSubscribe(getRefreshSubscriber());
    }

    @Override
    public void onLoadMore() {
        DynamicModel.getInstance()
                .discoverDynamics(getView().getActivity(), 10, lastTime, 2)
                .compose(new ErrorTransform<>(ErrorTransform.ServerErrorHandler.TOAST))
                .doOnNext(list -> {
                    if (list != null && list.size() > 0)
                        lastTime = list.get(list.size() - 1).getPublishTime();
                })
                .doAfterTerminate(() -> getAdapter().notifyDataSetChanged())
                .unsafeSubscribe(getMoreSubscriber());

    }
}
